package com.wuba.rxjavademo.tzx.create

import com.wuba.rxjavademo.tzx.common.Base
import rx.Observable
import kotlin.jvm.JvmStatic
import rx.Subscriber
import java.util.ArrayList

/**
 * Created by Tanzhenxing
 * Date: 2021/11/30 4:14 下午
 * Description:
 * cache之后数据不会发生变化，因为数据在第一次发射出去的时候进行缓存了
 */
object RxCache : Base() {
    @JvmStatic
    fun main(args: Array<String>) {
        val observable: Observable<String>
        val list: MutableList<String> = ArrayList()
        list.add("1111111")
        list.add("2222222")
        list.add("3333333")
        observable = Observable
            .from(list)
            .cache(1)
        list.removeAt(0)
        observable.subscribe(object : Subscriber<String?>() {
            override fun onCompleted() {
                print("onCompleted:")
            }

            override fun onError(e: Throwable) {
                print("onError:$e")
            }

            override fun onNext(s: String?) {
                print("onNext:$s")
            }
        })
        list.clear()
        observable.subscribe(object : Subscriber<String?>() {
            override fun onCompleted() {
                print("onCompleted:")
            }

            override fun onError(e: Throwable) {
                print("onError1:$e")
            }

            override fun onNext(s: String?) {
                print("onNext1:$s")
            }
        })
    }
}